package edu.neu.swc.cloud.service.impl;

import edu.neu.swc.cloud.dao.BideDao;
import edu.neu.swc.cloud.dao.FactoryDao;
import edu.neu.swc.cloud.dao.OrderDao;
import edu.neu.swc.cloud.domain.Bide;
import edu.neu.swc.cloud.domain.Factory;
import edu.neu.swc.cloud.domain.Order;
import edu.neu.swc.cloud.service.BideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("bideService")
public class BideServiceImpl implements BideService {

    @Autowired
    OrderDao orderDao;

    @Autowired
    FactoryDao factoryDao;

    @Autowired
    BideDao bideDao;

    public List<Bide> findAll(){
        List<Bide> bides = bideDao.findAll();
        for(Bide bide : bides){
            Factory bid_fac = factoryDao.findFacById(bide.getBid_fac_id());
            Order bid_ord = orderDao.findOrdById(bide.getBid_ord_id());
            bide.setBid_fac(bid_fac);
            bide.setBid_order(bid_ord);
        }
        return bides;
    }

    @Override
    public List<Bide> findBideByOrdId(int ord_id) {
        List<Bide> bideList = bideDao.findBideByOrdId(ord_id);
        for(Bide bide : bideList){
            Factory factory = factoryDao.findFacById(bide.getBid_fac_id());
            Order order = orderDao.findOrdById(bide.getBid_ord_id());
            bide.setBid_fac(factory);
            bide.setBid_order(order);
        }
        return bideList;
    }

    @Override
    public void addBide(Bide bide) {
        bideDao.addBide(bide);
    }

    @Override
    public Bide findBideByOrdAndFacId(int fac, int ord) {
        return bideDao.findBideByOrdAndFacId(fac,ord);
    }

    @Override
    public int getBideCounts(int ord_id) {
        return bideDao.getBideCounts(ord_id);
    }

    @Override
    public List<Bide> findBideByQuery(int ord_id, int pageStart, int pageSize) {
        List<Bide> bideList = bideDao.findBideByQuery(ord_id,pageStart,pageSize);
        for(Bide bide : bideList){
            Factory factory = factoryDao.findFacById(bide.getBid_fac_id());
            bide.setBid_fac_name(factory.getFa_name());
            bide.setBid_fac_conn(factory.getFa_spon());
            bide.setBid_fac_tel(factory.getFa_conn());
        }
        return bideList;
    }
}
